package cc.linker.steplink.crm.merchant.service.impl;


import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.coupon.CouponRequest;
import cc.linker.steplink.crm.commons.form.FormRequest;
import cc.linker.steplink.crm.commons.model.FormLoginDto;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.oss.OssService;
import cc.linker.steplink.crm.merchant.dao.ShopGoodsMapper;
import cc.linker.steplink.crm.merchant.dao.acs.PageDao;
import cc.linker.steplink.crm.merchant.dao.integral.IntegralGoodsDao;
import cc.linker.steplink.crm.merchant.service.acs.PageService;
import cc.linker.steplink.crm.merchanth5.commons.FormFilter;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.dao.acs.MerchantSalerDao;
import cc.linker.steplink.crm.merchanth5.service.acs.MerchantSalerService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private PageDao pageDao;

    @Autowired
    private MerchantSalerDao merchantSalerDao;
    @Autowired
    private MerchantSalerService merchantSalerService;

    @Autowired
    private ShopGoodsMapper shopGoodsMapper;

    @Autowired
    private OssService ossService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private FormFilter formFilter;

    @Autowired
    private FormRequest formRequest;

    @Autowired
    private CouponRequest couponRequest;

    @Autowired
    private UserInfoUtil userInfoUtil;

    @Autowired
    private IntegralGoodsDao integralGoodsDao;

    public Boolean addModule(Map<String, Object> param) {
        return pageDao.addModule(param);
    }

    @Override
    public Boolean updateModule(Map<String, Object> param) {
        return pageDao.updateModule(param);
    }

    // 插入自定义的商品组件
    @Override
    public void addGoodsList(Map<String, Object> param) {
        pageDao.delModuleImage(param.get("id").toString());
        pageDao.addGoodsList(param);
    }

    @Override
    public void addImagemodule(Map<String, Object> param) {
        String id = (param.containsKey("id") && !StringUtils.isEmpty(param.get("id"))) ?
                param.get("id").toString() : UUID.randomUUID().toString();
        if (param.containsKey("id") && !StringUtils.isEmpty(param.get("id"))) {
            id = param.get("id").toString();
            // 更新 t_crm_acs_org_module 表
            pageDao.updateModule(param);
            pageDao.delModuleImage(id);
        } else {
            // 新增
            param.put("id", id);
            param.put("sort", StringUtils.isEmpty(param.get("sort")) ? 1 : param.get("sort"));
            // 插入 t_crm_acs_org_module 表
            pageDao.addModule(param);
        }
        // 插入 t_crm_acs_org_module_image 表
        List<Map<String, Object>> temps = (List<Map<String, Object>>) param.get("contents");
        String finalId = id;
        temps.forEach(x -> x.put("moduleId", finalId));
        // 根据类型判断 如果是报名表单  v1.11.15
        if ("5".equals(param.get("moduleType").toString())) {
            pageDao.addEnrollImage(temps);
        } else {
            pageDao.addModuleImage(temps);
        }
    }

    @Override
    public String getCtimeByOrgId(String orgId) {
        return pageDao.getCtimeByOrgId(orgId);
    }

    @Override
    public Map<String, Object> getSaleInfoByAdminId(String adminId) {
        return pageDao.getSaleInfoByAdminId(adminId);
    }

    @Override
    public List<Map<String, Object>> moduleList(String customerId) {
        return pageDao.moduleList(customerId);
    }

    @Override
    public Map<String, Object> getGoodsModule(Map<String, Object> param) {
        Map<String, Object> result = pageDao.getGoodsModule(param);
        result.put("goodsList", pageDao.moduleGoodsList(param));
        return result;
    }

    @Override
    public void delModule(String id, String customerId) {
        pageDao.delModule(id, customerId);
        pageDao.delModuleImage(id);
    }

    @Override
    public Map<String, Object> getModule(String id) {
        return pageDao.getModule(id);
    }

    @Override
    public List<Map<String, Object>> getModuleImage(String id) {
        return pageDao.getModuleImage(id);
    }

    @Override
    public void sortModule(List<Map<String, Object>> param, String customerId) {
        param.forEach(x -> x.put("customerId", customerId));
        pageDao.sortModule(param);
    }

    @Override
    public Map<String, Object> getEnrollModule(String id) {
        return pageDao.getEnrollModule(id);
    }

    @Override
    public List<Map<String, Object>> getEnrollImage(String id) {
        return pageDao.getEnrollImage(id);
    }

    @Override
    public List<Map<String, Object>> goodsList(Map<String, Object> param) {
        List<Map<String, Object>> list = pageDao.goodsList(param);
        list.forEach(x -> {
            // 查询关联的标签和名称
            List<Map<String, Object>> labels = shopGoodsMapper.getGoodsLabelsById(Long.parseLong(x.get("goodsId").toString()));
            x.put("labels", labels);
        });
        return list;
    }

    @Override
    public List<Map<String, Object>> moduleGoodsList(Map<String, Object> param) {
        return pageDao.moduleGoodsList(param);
    }

    /****************************新版页面装修功能**v1.12.6版本***************************************************/

    @Override
    public void deletePage(Integer id, String customerId, String orgId) {
        // 判断是否绑定首页 ，已绑定不能删除
        Map<String, Object> pageInfo = pageDao.getPage(id);
        if (pageInfo == null || "1".equals(pageInfo.get("isHome").toString())) {
            String error = "该页面已设为我的小店，不可删除";
            if (customerId.equals(orgId)) {
                // 大麦
                error = "该页面已设为首页，不可删除";
            }
            throw GlobalException.build(error);
        }
        // 判断是否绑定导航 ，已绑定不能删除
        int ifBind = pageDao.checkIfBindFooter(id, customerId);
        if (ifBind > 0) {
            throw GlobalException.build("该页面已绑定底部导航，不可删除");
        }
        pageDao.deletePage(id, customerId);
        // 删除缓存
        String key = "pageInfo:" + id + ":" + 0;
        String key1 = "pageInfo:" + id + ":" + 1;
        stringRedisTemplate.delete(key);
        stringRedisTemplate.delete(key1);
    }

    @Override
    public List<Map<String, Object>> pageList(Map<String, Object> param) {
        return pageDao.pageList(param);
    }

    @Override
    public void savePage(Map<String, Object> param) throws IOException {
        String customerId = param.get("customerId").toString();
        String orgId = param.get("orgId").toString();
        // 判断页面描述字符 最大500
        if (param.containsKey("pageInfo") && !StringUtils.isEmpty(param.get("pageInfo"))) {
            //
            String pageInfo = param.get("pageInfo").toString();
            if (pageInfo.length() > 500) {
                throw GlobalException.build("页面描述字数不能超过500！");
            }
        }
        // 保存page
        Integer id = null;
        if (StringUtils.isEmpty(param.get("pageImageUrl")) && StringUtils.isEmpty(param.get("markings"))) {
            throw GlobalException.build("base64图片未上传！");
        }
        if (!StringUtils.isEmpty(param.get("pageImageUrl"))) {
            try {
                //  处理图片 base64 转存到oss后返回地址 存入数据库
                BASE64Decoder decode = new BASE64Decoder();
                byte[] oldBytes = decode.decodeBuffer(param.get("pageImageUrl").toString());
                InputStream fileInputStream = new ByteArrayInputStream(oldBytes);
                String pageImage = ossService.uploadFileToOssWithInputStream("Steplink", "Steplink", fileInputStream, "page.jpg");
                param.put("pageImageUrl", pageImage);
            } catch (Exception e) {
                throw GlobalException.build("预览图上传oss失败！");
            }
        }
        // todo 判断名称是否相同
//        int countSame = pageDao.checkSameName(param);
//        if(countSame>0){
//            throw GlobalException.build("页面名称已存在，请修改。");
//        }

        if (StringUtils.isEmpty(param.get("id"))) {
            // 新增
            param.put("isHome", 0);
            pageDao.insertPage(param);
            id = Integer.parseInt(param.get("id").toString());
        } else {
            id = Integer.parseInt(param.get("id").toString());
            // 编辑
            pageDao.updatePage(param);
            // 删除原 module
            pageDao.delPageModule(id);
            // 删除原 moduleImage
            pageDao.delPageModuleImage(id);
            if (!StringUtils.isEmpty(param.get("markings"))) {
                List<Map> markings = (List<Map>) param.get("markings");
                if (markings != null && markings.size() > 0) {
                    pageDao.delMarking(orgId);
                    //新增
                    pageDao.insertMarking(markings, orgId);
                    String keys = "marking:" + orgId;
                    stringRedisTemplate.delete(keys);
                }
                String userCenterkey = "userCenter:" + orgId;
                stringRedisTemplate.delete(userCenterkey);
            }
        }

        List<Map<String, Object>> moduleList = (List<Map<String, Object>>) param.get("moduleList");
        // 判断是否传入了模块
        if (StringUtils.isEmpty(moduleList) || moduleList.size() < 1) {
            return;
        }
        // 保存module
        int moduleSort = 0;
        for (Map<String, Object> module : moduleList) {
            moduleSort = moduleSort + 1;
            // 判断传入模块参数是否包含 类型
            if (module.size() < 1 || StringUtils.isEmpty(module.get("moduleType"))) {
                continue;
            }
            // 判断 分组是否存在数组
            if ((("3".equals(module.get("moduleType").toString()) || "4".equals(module.get("moduleType").toString())) && "2".equals(module.get("showType").toString()))
                    || "8".equals(module.get("moduleType").toString()) || "6".equals(module.get("moduleType").toString()) || "9".equals(module.get("moduleType").toString())) {
                if (module.containsKey("goodsGroupId") && null != module.get("goodsGroupId") && !"".equals(module.get("goodsGroupId").toString())) {
                    // 判断分组是否有数据
                    int countImage = this.getGoodsGroupNum(Integer.valueOf(module.get("goodsGroupId").toString()));
                    if (countImage < 1) {
                        throw GlobalException.build("添加失败，所选分组没有数据");
                    }
                }
            }
            if (StringUtils.isEmpty(module.get("goodsGroupId"))) {
                module.remove("goodsGroupId");
            }
            // 生成模块ID
            String moduleId = UUID.randomUUID().toString();
            module.put("id", moduleId);
            module.put("pageId", id);
            module.put("customerId", customerId);
            module.put("orgId", orgId);
            module.put("sort", moduleSort);
            pageDao.insertPageModule(module);
            // 保存moduleImage 判断模块类型


            List<Map<String, Object>> imageList = (List<Map<String, Object>>) module.get("imageList");
            // 判断模块内容是否存在
            if (StringUtils.isEmpty(imageList) || imageList.size() < 1) {
                continue;
            }
            int goodsSort = 0;
            for (Map<String, Object> image : imageList) {
                goodsSort = goodsSort + 1;
                if (image.size() < 1) {
                    continue;
                }
                image.put("moduleId", moduleId);
                image.put("goodsSort", goodsSort);
                pageDao.insertPageImageModule(image);
            }
        }
    }

    @Override
    public Map<String, Object> getPage(Integer id, String customerId) {
        // 获取页面配置
        Map<String, Object> pageInfo = pageDao.getPage(id);
        // 获取页面模块
        List<Map<String, Object>> moduleList = pageDao.getModuleList(id);
        //
        for (Map<String, Object> module : moduleList) {
            List<Map<String, Object>> imageList = pageDao.getImageList(module.get("id").toString());
            module.put("imageList", imageList);
        }
        pageInfo.put("moduleList", moduleList);
        return pageInfo;
    }

    @Override
    public void saveFooter(Map<String, Object> param) {
        //
        if (StringUtils.isEmpty(param.get("id"))) {
            // 新增
            pageDao.saveFooter(param);
            pageDao.saveFooterItem(param);
        } else {
            // 修改
            pageDao.updateFooter(param);
            pageDao.delFooterItem(param);
            pageDao.saveFooterItem(param);
        }
    }

    @Override
    public Map<String, Object> getFooter(String customerId) {
        Map<String, Object> result = merchantSalerDao.getFooter(customerId);
        if (StringUtils.isEmpty(result)) {
            // todo 返回默认的导航
            throw GlobalException.build("导航数据未配置");
        }
        List<Map<String, Object>> itemList = merchantSalerDao.getFooterItem(Integer.parseInt(result.get("id").toString()));
        result.put("itemList", itemList);
        return result;
    }

    @Override
    public List<Map<String, Object>> getPageList(String customerId, Integer pageKind) {
        return pageDao.getPageList(customerId, pageKind);
    }

    /**
     * 初始化 大麦或者联盟商家登录时初始化
     *
     * @param customerId
     * @param orgId
     */
    @Override
    public void setInitializePage(String customerId, String orgId) {

        Integer id = pageDao.getPageIdByCustomerId(customerId);
        if (StringUtils.isEmpty(id)) {
            // 判断大麦和联盟商家
            // 插入空白模板
            Map<String, Object> page1 = new HashMap<>();
            page1.put("customerId", customerId);
            page1.put("orgId", orgId);
            page1.put("pageKind", 0);
            page1.put("pageName", "");
            page1.put("pageTitle", "空白");
            page1.put("pageInfo", "");
            page1.put("pageColour", "");
            page1.put("pageType", 3);
            page1.put("pageImageUrl", "");
            page1.put("isHome", 0);
            pageDao.insertPage(page1);

            if (!customerId.equals(orgId)) {
                //  插入首页
                Map<String, Object> pageParam = new HashMap<>();
                pageParam.put("customerId", customerId);
                pageParam.put("orgId", orgId);
                pageParam.put("pageKind", 0);
                pageParam.put("pageName", "首页");
                pageParam.put("pageTitle", "首页");
                pageParam.put("pageInfo", "推荐你看看，精选好货等你选!");
                pageParam.put("pageColour", "#F5F5F5");
                pageParam.put("pageType", 1);
                pageParam.put("pageImageUrl", "");
                pageParam.put("isHome", 1);
                pageDao.insertPage(pageParam);
            } else {
                // 初始化 页面和底部导航
                // 插入footer
                Map<String, Object> footerParam = new HashMap<>();
                footerParam.put("orgId", orgId);
                footerParam.put("customerId", customerId);
                footerParam.put("name", "底部导航");
                footerParam.put("normalWordColour", "#909399");
                footerParam.put("chooseWordColour", "#FFB51E");
                pageDao.saveFooter(footerParam);
                for (int i = 0; i < 3; i++) {
                    // 插入page
                    Map<String, Object> pageParam = new HashMap<>();
                    pageParam.put("customerId", customerId);
                    pageParam.put("orgId", orgId);
                    int pageKind = 0;
                    String pageName = "首页";
                    String pageTitle = "商城首页";
                    String pageColour = "#F5F5F5";
                    String pageImageUrl = "";
                    int isHome = 1;
                    if (i == 1) {
                        pageKind = -1;
                        pageName = "购物车";
                        pageTitle = "购物车";
                        pageColour = "";
                        pageImageUrl = "https://incloud.linker.cc/StepLink/202008/20/10/D0XSZqWuPlaWgWeuFN7U12Na16adTvT2yx3wCuAjMad2020082010616.png";
                        isHome = 0;
                    } else if (i == 2) {
                        pageKind = -2;
                        pageName = "个人中心";
                        pageTitle = "个人中心";
                        pageColour = "";
                        pageImageUrl = "https://incloud.linker.cc/StepLink/202008/04/11/h8kDEx6UXvQUrcywdSBXid0xcX3HlKUdHrFhrjT2020080411501.png";
                        isHome = 0;
                    }
                    pageParam.put("pageKind", pageKind);
                    pageParam.put("pageName", pageName);
                    pageParam.put("pageTitle", pageTitle);
                    pageParam.put("pageInfo", "推荐你看看，精选好货等你选!");
                    pageParam.put("pageColour", pageColour);
                    pageParam.put("pageType", 1);
                    pageParam.put("pageImageUrl", pageImageUrl);
                    pageParam.put("isHome", isHome);
                    pageDao.insertPage(pageParam);

                    // 插入footerItem
                    Map<String, Object> footerItemParam = new HashMap<>();
                    String footItemName = "首页";
                    String normalImage = "https://incloud.linker.cc/StepLink/202007/28/18/VRI2020072818778.png";
                    String chooseImage = "https://incloud.linker.cc/StepLink/202007/28/18/KMU99kf2020072818224.png";
                    if (i == 1) {
                        footItemName = "购物车";
                        normalImage = "https://incloud.linker.cc/StepLink/202007/28/18/Mz3ezqydNgV0N2020072818771.png";
                        chooseImage = "https://incloud.linker.cc/StepLink/202007/28/18/7RXELBlkwv2020072818848.png";
                    } else if (i == 2) {
                        footItemName = "我的";
                        normalImage = "https://incloud.linker.cc/StepLink/202007/28/18/kJIcXLPImvJKWEBe7Eek2qccjE8aam5AVKPZE0iDraQ2020072818208.png";
                        chooseImage = "https://incloud.linker.cc/StepLink/202007/28/18/W4U3uakNzL1zbJJ8mvClGzzNFi2020072818575.png";
                    }
                    footerItemParam.put("pageId", pageParam.get("id"));
                    footerItemParam.put("footerId", footerParam.get("id"));
                    footerItemParam.put("name", footItemName);
                    footerItemParam.put("normalImage", normalImage);
                    footerItemParam.put("chooseImage", chooseImage);
                    footerItemParam.put("sort", i + 1);
                    pageDao.insertFooterItem(footerItemParam);
                }
            }
        }
    }

    @Override
    public void setIndexPage(Integer id, String customerId) {
        pageDao.cancleIndePage(customerId);
        pageDao.setIndexPage(id, customerId);
    }

    @Override
    public Map<String, Object> saveEditPage(Map<String, Object> param, HttpServletRequest request) {
        String customerId = param.get("customerId").toString();
        String orgId = param.get("orgId").toString();
        List<Map<String, Object>> moduleList = (List<Map<String, Object>>) param.get("moduleList");
        for (Map<String, Object> module : moduleList) {
            int isAll = 0;
            module.put("goodsCount", 0);
            module.put("enrollCount", 0);
            int moduleType = Integer.parseInt(module.get("moduleType").toString());
            // 针对不同模块获取对应的数据
            switch (moduleType) {
                case 1:
                    // 图片广告
                case 2:
                    // 图文导航
                    List<Map<String, Object>> items = (List<Map<String, Object>>) module.get("imageList");
                    module.put("items", items);
                    break;
                case 10:
                    // 积分商品
                case 3:
                    // 商品
                case 4:
                    // 知识商品
                    if (moduleType == 10 && !param.get("orgId").equals(param.get("customerId"))){
                        isAll = 1;
                    }
                    this.getGoodsItems(orgId, customerId, module, moduleType, module.get("isAll") == null ? isAll : Integer.parseInt(module.get("isAll").toString()));
                    break;
                case 5:
                    //报名
                    List<Map<String, Object>> enrollList = new ArrayList<>();
                    Integer enrollCount = 0;
                    List<Map<String, Object>> list = (List<Map<String, Object>>) module.get("imageList");
                    if (list != null && list.size() > 0) {
                        module.put("isAll", isAll);
                        enrollList = pageDao.getPageEnrollList(module);
                        if (null != enrollList && enrollList.size() > 0) {
                            for (Map<String, Object> enroll : enrollList) {
                                int status = merchantSalerService.getEnrollStatus((String) enroll.get("enrollId"));
                                enroll.put("status", status);
                            }
                        }
                        enrollCount = pageDao.getEnrollCount(module);
                        module.remove("isAll");
                    }
                    module.put("enrollCount", enrollCount);
                    module.put("items", enrollList);
                    break;
                case 6:
                    //优惠券
                    List<Map<String, Object>> listc = (List<Map<String, Object>>) module.get("imageList");
                    if (listc != null && listc.size() > 0) {
                        List<Map<String, Object>> couponListMap = new ArrayList<>();
                        List<String> couponIdList = new ArrayList<>();
                        for (Map c : listc) {
                            couponIdList.add(c.get("couponId").toString());
                        }
                        couponListMap = this.getCouponListByCouponIds(couponIdList, request);
                        List<Map<String, Object>> couponListMaps = new ArrayList<>();
                        for (String couponId : couponIdList) {
                            for (Map couponMap : couponListMap) {
                                if (couponId.equals(couponMap.get("couponId").toString())) {
                                    couponListMaps.add(couponMap);
                                }
                            }
                        }
                        module.put("items", couponListMaps);
                    }
                    break;
                case 7:
                    //表单
                    List<Map<String, Object>> listb = (List<Map<String, Object>>) module.get("imageList");
                    if (listb != null && listb.size() > 0) {
                        List<Map<String, Object>> newEnrollListMap = new ArrayList<>();
                        List<String> newEnrollList = new ArrayList<>();
                        for (Map b : listb) {
                            newEnrollList.add(b.get("newEnrollId").toString());
                        }
                        FormLoginDto formLoginDto = formFilter.getFormLoginDto(request, false);
                        Map params = new HashMap();
                        String newStr = newEnrollList.stream().collect(Collectors.joining(","));
                        params.put("ids", newStr);
                        List<Map<String, Object>> listv = (List<Map<String, Object>>) formRequest.doRequest("/pc/form/templates/getAllList", params, formLoginDto);
                        if (listv != null && listv.size() > 0) {
                            Integer showNum = (Integer) module.get("showNum");
                            Integer count = 0;
                            for (Map newEnroll : listv) {
                                if (count < showNum) {
                                    //enrollId,title,shareContent,cover
                                    Map enrollMap = new HashMap();
                                    enrollMap.put("formId", newEnroll.get("formId"));
                                    enrollMap.put("formTitle", newEnroll.get("formTitle"));
                                    enrollMap.put("shareContent", newEnroll.get("shareContent"));
                                    enrollMap.put("cover", newEnroll.get("cover"));
                                    enrollMap.put("status", newEnroll.get("status"));
                                    String statusName = "待报名";
                                    if (!StringUtils.isEmpty(newEnroll.get("statusName"))) {
                                        statusName = newEnroll.get("statusName").toString();
                                        if ("报名中".equals(statusName)) {
                                            //如果是报名中 判断是否  报名已满
                                            if (!StringUtils.isEmpty(newEnroll.get("formId"))) {
                                                Map map = new HashMap();
                                                map.put("id", Long.valueOf(newEnroll.get("formId").toString()));
                                                Map resmap = (Map) formRequest.doRequest("/pc/form/templates/getFormStatus", map, formLoginDto);
                                                if (resmap != null && !StringUtils.isEmpty(resmap.get("statusName"))) {
                                                    statusName = resmap.get("statusName").toString();
                                                }
                                            }
                                        }

                                    }
                                    enrollMap.put("statusName", statusName);
                                    newEnrollListMap.add(enrollMap);
                                } else {
                                    break;
                                }
                                count++;
                            }
                            module.put("formCount", listv.size());
                            module.put("items", newEnrollListMap);
                        }
                    }
                    break;
                case 8:
                    //视频
                    List<Map<String, Object>> videos = (List<Map<String, Object>>) module.get("imageList");
//                    List<Map<String, Object>> videos = merchantSalerDao.getVideoModuleImageListById(module.get("id").toString());
                    module.put("items", videos);
                    break;
                case 9:
                    //权益卡
//                    List<Map<String, Object>> vipList = merchantSalerDao.getPageVipList((String) module.get("id"));
                    List<Map<String, Object>> vipList = (List<Map<String, Object>>) module.get("imageList");
                    module.put("items", vipList);
                    break;
                default:
                    break;
            }
            module.remove("imageList");
        }
        return param;
    }

    //获取优惠券
    @Override
    public List<Map<String, Object>> getCouponListByCouponIds(List<String> couponIds, HttpServletRequest request) {
        PcUserInfo userInfo = userInfoUtil.getPcUserInfo(request);
        CouponCustomerInfo customerInfo = CouponCustomerInfo
                .builder()
                .customerId(userInfo.getSaler() ? userInfo.getCustomerId() : userInfo.getOrgId())
                .customerName(userInfo.getAdminName())
                .customerPhone("15012345678")
                .cover("")
                .orgId(userInfo.getOrgId())
                .build();
        //查询符合该商品的优惠券
        Map<String, Object> param = new HashMap<>();
        List<Map<String, Object>> dataList = null;
        if (couponIds != null && couponIds.size() > 0) {
            param.put("couponIds", String.join(",", couponIds));
            Object list = couponRequest.doRequest("coupon-all-list", param, customerInfo);
            dataList = (List<Map<String, Object>>) list;
            if (null != dataList && dataList.size() > 0) {
                for (Map<String, Object> da : dataList) {
                    Map<String, Object> couponGoodsData = merchantSalerDao.getCouponGoodsData(da.get("couponId").toString());
                    if (null != couponGoodsData) {
                        da.put("customerType", couponGoodsData.get("customerType"));
                        da.put("goodsType", couponGoodsData.get("goodsType"));
                        da.put("customerCouponNum", couponGoodsData.get("customerCouponNum"));
                    }
                }
            }
        }
        if (dataList == null) {
            dataList = new ArrayList<>();

        }
        return dataList;
    }

    private void getGoodsItems(String orgId, String customerId, Map<String, Object> module, Integer moduleType, Integer isAll) {
        String tipsShow = module.get("tipsShow").toString();
        if ("0".equals(tipsShow)) {
            module.put("goodsTitle", "");
            module.put("goodsDesc", "");
        }
        int type;
        if (orgId.equals(customerId)) {
            // 大B
            type = 0;
        } else {
            // 小b
            type = 1;
        }
        Integer showType = Integer.valueOf(module.get("showType").toString());
        if (2 == showType) {
            List<Map<String, Object>> goodsList = new ArrayList<>();
            Integer goodsCount = 0;
            if (!StringUtils.isEmpty(module.get("goodsGroupId"))) {
                // 分组商品
                goodsList = merchantSalerDao.getGoodsByGroupId((Integer) module.get("goodsGroupId"), (Integer) module.get("showNum"), orgId, type, customerId, moduleType, isAll);
                for (Map<String, Object> go : goodsList) {
                    if ("1".equals(go.get("saler").toString())) {
                        go.put("isShare", "1");
                    } else {
                        go.put("isShare", "0");
                    }
                    go.put("goodsPrice", go.get("moneyAmount"));
                    if (!StringUtils.isEmpty(go.get("isSeckill")) && "1".equals(go.get("isSeckill").toString())) {
                        merchantSalerService.getKillThings(go);
                    }
                }
                goodsCount = merchantSalerDao.getGoodsCountByGroupId((Integer) module.get("goodsGroupId"), type, customerId, orgId, moduleType);
            }
            module.put("items", goodsList);
            module.put("goodsCount", goodsCount);
        } else if (1 == showType) {
            List<Map<String, Object>> goodsList = new ArrayList<>();
            Integer goodsCount = 0;
            List<Map<String, Object>> imageList = (List) module.get("imageList");
            if (imageList.size() > 0) {
                // 自定义商品
                Map<String, Object> paramCustom = new HashMap<>();
                paramCustom.put("moduleType", module.get("moduleType"));
                Object showNum = module.get("showNum");
                Integer limit;
                if (null == showNum) {
                    limit = 0;
                } else {
                    limit = Integer.valueOf(module.get("showNum").toString());
                }
                paramCustom.put("showNum", limit);
                paramCustom.put("type", type);
                paramCustom.put("customerId", customerId);
                paramCustom.put("orgId", orgId);
                paramCustom.put("isAll", isAll);
                paramCustom.put("goodsIds", module.get("imageList"));
                if (Integer.parseInt(module.get("moduleType").toString()) == 10) {
                    goodsList = integralGoodsDao.getListIntGoodsCustom(paramCustom);
                    goodsCount = integralGoodsDao.getListIntGoodsCustomCount(paramCustom);
                } else {
                    goodsCount = pageDao.getListGoodsCustomCount(paramCustom);
                    goodsList = pageDao.getListGoodsCustom(paramCustom);
                }
                for (Map<String, Object> go : goodsList) {
                    if ("1".equals(go.get("saler").toString())) {
                        go.put("isShare", "1");
                    } else {
                        go.put("isShare", "0");
                    }
                    go.put("goodsPrice", go.get("moneyAmount"));
                    if (!StringUtils.isEmpty(go.get("isSeckill")) && "1".equals(go.get("isSeckill").toString())) {
                        merchantSalerService.getKillThings(go);
                    }
                }
            }
            module.put("goodsCount", goodsCount);
            module.put("items", goodsList);
        }
    }

    @Override
    public int getGoodsGroupNum(Integer goodsGroupId) {
        int countGoods = pageDao.countGoodsGroup(goodsGroupId);
        // 联盟商品
        int countgroup = pageDao.countCustomerGoodsGroup(goodsGroupId);
        return countGoods + countgroup;
    }
}
